package crypto.keypairgenerator;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class KeyPairGenertor {

    public static void main(String args[]) throws Exception {

        doKeyGenerator();
    }

    private static void do1() throws Exception {
        //Creating KeyPair generator object
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");

        //Initializing the KeyPairGenerator
        keyPairGen.initialize(2048);

        //Generating the pair of keys
        KeyPair pair = keyPairGen.generateKeyPair();

        //Getting the private key from the key pair
        PrivateKey privKey = pair.getPrivate();

        //Getting the public key from the key pair
        PublicKey publicKey = pair.getPublic();
        System.out.println("Keys generated");
        System.out.println(privKey.getFormat());
        System.out.println(publicKey.getFormat());
    }

    private static void doKeyGenerator() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

        SecretKey secretKey = keyGenerator.generateKey();

        Cipher cipher = Cipher.getInstance("DES");

        cipher.init(Cipher.WRAP_MODE, secretKey);

        byte[] k = cipher.wrap(secretKey);

        cipher.init(Cipher.UNWRAP_MODE, secretKey);

        Key key = cipher.unwrap(k, "DES", Cipher.SECRET_KEY);

        System.out.println(key.equals(secretKey));

        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] input = cipher.doFinal("DES DATA".getBytes());

        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        byte[] output = cipher.doFinal(input);
    }
}
